<?php
class TdFormField extends TdComponent {
  public $value;
  public $cssclass;
  public $divcssclass;
  public $errormessage;
  public $form;
  public $required = FALSE;
  public $label;
  public $name;
  public $jsform;
  
  function __construct($id, $template="") {
    parent::__construct($id, $template);
    
    // default name is id
    $this->setName($id);
  }

  function setClass($value) {
    $this->cssclass = $value;
  }
  function getClass() {
    return $this->cssclass;
  }
  
  function setDivClass($value) {
  	$this->divcssclass = $value;
  }
  function getDivClass() {
  	return $this->divcssclass;
  }

  function setValue($value) {
  	$this->value = $value;
  }
  
  function getValue() {
  	return $this->value;
  }
  
  function setName($value) {
  	$this->name = $value;
  }
  
  function getName() {
  	return $this->name;
  }
  
  function gotError() {
  	return isset($this->errormessage) && $this->errormessage != "";
  }
  
  function addErrorMessage($message) {
  	if ($this->errormessage != "") {
  		$this->errormessage .= " | ";
  	}
  	$this->errormessage .= $message;
  }
  
  function setErrorMessage($message) {
    $this->errormessage = $message;
  }
  
  function getErrorMessage() {
  	return $this->errormessage;
  }
  
  function setForm($value) {
  	$this->form = $value;
  }
  
  function getForm() {
  	return $this->form;
  }
  
  function setRequired($value) {
  	$this->required = $value;
  }
  
  function getRequired() {
  	return $this->required;
  }
  
  function setLabel($obj) {
  	$obj->setFor($this->id);
  	$this->label = $obj;
  }
  
  function getLabel() {
  	return $this->label;
  }
  
  function setJs($string) {  	
  	$this->jsform = $string;
  }
  
  function getJs() {
  	return $this->jsform;
  }
  
  function doValidate() {
  	if (isset($this->arguments[$this->id])) {
  		$this->setValue($this->arguments[$this->id]);
  	}
  	
  	if (($this->getRequired()) && ($this->getValue() == "")) {
  		if (isset($this->label)) {
  		  $this->addErrorMessage("Field ".$this->getLabel()->getValue()." is required.");
  		}
  		else {
  			$this->addErrorMessage("Field ".$this->id." is required.");
  		}
  	}
  }
}
?>